



<!doctype html>
<html lang="en" class="no-js">
  <head>
    
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width,initial-scale=1">
      <meta http-equiv="x-ua-compatible" content="ie=edge">
      
        <meta name="description" content="Here you can find all documents about QTrobot.">
      
      
        <link rel="canonical" href="https://luxai-qtrobot.github.io/demos/qt_microphone_interaction/">
      
      
        <meta name="author" content="QTrobot">
      
      
        <meta name="lang:clipboard.copy" content="Copy to clipboard">
      
        <meta name="lang:clipboard.copied" content="Copied to clipboard">
      
        <meta name="lang:search.language" content="en">
      
        <meta name="lang:search.pipeline.stopwords" content="True">
      
        <meta name="lang:search.pipeline.trimmer" content="True">
      
        <meta name="lang:search.result.none" content="No matching documents">
      
        <meta name="lang:search.result.one" content="1 matching document">
      
        <meta name="lang:search.result.other" content="# matching documents">
      
        <meta name="lang:search.tokenizer" content="[\s\-]+">
      
      <link rel="shortcut icon" href="../../img/luxai-favicon.ico">
      <meta name="generator" content="mkdocs-1.1, mkdocs-material-4.6.3">
    
    
      
        <title>**Microphone Voice Interaction** - QTrobot Documentation</title>
      
    
    
      <link rel="stylesheet" href="../../assets/stylesheets/application.adb8469c.css">
      
        <link rel="stylesheet" href="../../assets/stylesheets/application-palette.a8b3c06d.css">
      
      
        
        
        <meta name="theme-color" content="#3f51b5">
      
    
    
      <script src="../../assets/javascripts/modernizr.86422ebf.js"></script>
    
    
      
        <link href="https://fonts.gstatic.com" rel="preconnect" crossorigin>
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,400i,700%7CRoboto+Mono&display=fallback">
        <style>body,input{font-family:"Roboto","Helvetica Neue",Helvetica,Arial,sans-serif}code,kbd,pre{font-family:"Roboto Mono","Courier New",Courier,monospace}</style>
      
    
    <link rel="stylesheet" href="../../assets/fonts/material-icons.css">
    
    
      <link rel="stylesheet" href="../../stylesheets/extra.css">
    
    
      
    
    
  </head>
  
    
    
    <body dir="ltr" data-md-color-primary="indigo" data-md-color-accent="indigo">
  
    <svg class="md-svg">
      <defs>
        
        
      </defs>
    </svg>
    <input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
    <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
    <label class="md-overlay" data-md-component="overlay" for="__drawer"></label>
    
      <a href="#microphone-voice-interaction" tabindex="0" class="md-skip">
        Skip to content
      </a>
    
    
      <header class="md-header" data-md-component="header">
  <nav class="md-header-nav md-grid">
    <div class="md-flex">
      <div class="md-flex__cell md-flex__cell--shrink">
        <a href="https://luxai-qtrobot.github.io/" title="QTrobot Documentation" aria-label="QTrobot Documentation" class="md-header-nav__button md-logo">
          
            <img alt="logo" src="../../img/logo_luxai_white.png" width="24" height="24">
          
        </a>
      </div>
      <div class="md-flex__cell md-flex__cell--shrink">
        <label class="md-icon md-icon--menu md-header-nav__button" for="__drawer"></label>
      </div>
      <div class="md-flex__cell md-flex__cell--stretch">
        <div class="md-flex__ellipsis md-header-nav__title" data-md-component="title">
          
            <span class="md-header-nav__topic">
              QTrobot Documentation
            </span>
            <span class="md-header-nav__topic">
              
                **Microphone Voice Interaction**
              
            </span>
          
        </div>
      </div>
      <div class="md-flex__cell md-flex__cell--shrink">
        
          <label class="md-icon md-icon--search md-header-nav__button" for="__search"></label>
          
<div class="md-search" data-md-component="search" role="dialog">
  <label class="md-search__overlay" for="__search"></label>
  <div class="md-search__inner" role="search">
    <form class="md-search__form" name="search">
      <input type="text" class="md-search__input" aria-label="search" name="query" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="query" data-md-state="active">
      <label class="md-icon md-search__icon" for="__search"></label>
      <button type="reset" class="md-icon md-search__icon" data-md-component="reset" tabindex="-1">
        &#xE5CD;
      </button>
    </form>
    <div class="md-search__output">
      <div class="md-search__scrollwrap" data-md-scrollfix>
        <div class="md-search-result" data-md-component="result">
          <div class="md-search-result__meta">
            Type to start searching
          </div>
          <ol class="md-search-result__list"></ol>
        </div>
      </div>
    </div>
  </div>
</div>
        
      </div>
      
    </div>
  </nav>
</header>
    
    <div class="md-container">
      
        
      
      
      <main class="md-main" role="main">
        <div class="md-main__inner md-grid" data-md-component="container">
          
            
              <div class="md-sidebar md-sidebar--primary" data-md-component="navigation">
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    <nav class="md-nav md-nav--primary" data-md-level="0">
  <label class="md-nav__title md-nav__title--site" for="__drawer">
    <a href="https://luxai-qtrobot.github.io/" title="QTrobot Documentation" class="md-nav__button md-logo">
      
        <img alt="logo" src="../../img/logo_luxai_white.png" width="48" height="48">
      
    </a>
    QTrobot Documentation
  </label>
  
  <ul class="md-nav__list" data-md-scrollfix>
    
      
      
      


  <li class="md-nav__item">
    <a href="../.." title="Home" class="md-nav__link">
      Home
    </a>
  </li>

    
      
      
      


  <li class="md-nav__item">
    <a href="../../user-manual/" title="Quick Start" class="md-nav__link">
      Quick Start
    </a>
  </li>

    
      
      
      


  <li class="md-nav__item">
    <a href="../../api/" title="API Reference" class="md-nav__link">
      API Reference
    </a>
  </li>

    
      
      
      


  <li class="md-nav__item md-nav__item--nested">
    
      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-4" type="checkbox" id="nav-4">
    
    <label class="md-nav__link" for="nav-4">
      FAQ
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="1">
      <label class="md-nav__title" for="nav-4">
        FAQ
      </label>
      <ul class="md-nav__list" data-md-scrollfix>
        
        
          
          
          


  <li class="md-nav__item">
    <a href="../../FAQ/Audio/" title="Audio" class="md-nav__link">
      Audio
    </a>
  </li>

        
          
          
          


  <li class="md-nav__item">
    <a href="../../FAQ/Autostart/" title="Autostart" class="md-nav__link">
      Autostart
    </a>
  </li>

        
          
          
          


  <li class="md-nav__item">
    <a href="../../FAQ/Motors/" title="Motors" class="md-nav__link">
      Motors
    </a>
  </li>

        
          
          
          


  <li class="md-nav__item">
    <a href="../../FAQ/Network/" title="Network" class="md-nav__link">
      Network
    </a>
  </li>

        
          
          
          


  <li class="md-nav__item">
    <a href="../../FAQ/Miscellaneous/" title="Miscellaneous" class="md-nav__link">
      Miscellaneous
    </a>
  </li>

        
      </ul>
    </nav>
  </li>

    
      
      
      


  <li class="md-nav__item">
    <a href="../" title="Demos" class="md-nav__link">
      Demos
    </a>
  </li>

    
      
      
      


  <li class="md-nav__item">
    <a href="../../examples/" title="Examples" class="md-nav__link">
      Examples
    </a>
  </li>

    
      
      
      


  <li class="md-nav__item">
    <a href="../../author-note/" title="Author's Note" class="md-nav__link">
      Author's Note
    </a>
  </li>

    
  </ul>
</nav>
                  </div>
                </div>
              </div>
            
            
              <div class="md-sidebar md-sidebar--secondary" data-md-component="toc">
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    
<nav class="md-nav md-nav--secondary">
  
  
    
  
  
    <label class="md-nav__title" for="__toc">Table of contents</label>
    <ul class="md-nav__list" data-md-scrollfix>
      
        <li class="md-nav__item">
  <a href="#video" class="md-nav__link">
    Video
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#setup" class="md-nav__link">
    Setup
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#code" class="md-nav__link">
    Code
  </a>
  
</li>
      
      
      
      
      
    </ul>
  
</nav>
                  </div>
                </div>
              </div>
            
          
          <div class="md-content">
            <article class="md-content__inner md-typeset">
              
                
                
                <h1 id="microphone-voice-interaction"><strong>Microphone Voice Interaction</strong></h1>
<div class="admonition question">
<p class="admonition-title">About</p>
<p>This is microphone voice interaction demo with <a href="http://luxai.com/qtrobot-for-research/#hardware">QTrobot.</a>
Check the video for demonstration. Full code for this demo you can find in our <a href="https://github.com/luxai-qtrobot/tutorials/tree/master/demos/qt_microphone_interaction">github tutorial repository.</a></p>
</div>
<h2 id="video"><strong>Video</strong></h2>
<p><a href="http://www.youtube.com/watch?v=ol54IzyZOR0" title="Far Field Microphone Array - Voice Interaction"><img alt="Voice Interaction" src="http://img.youtube.com/vi/ol54IzyZOR0/0.jpg" /></a></p>
<h2 id="setup"><strong>Setup</strong></h2>
<div class="admonition warning">
<p class="admonition-title">NOTICE</p>
<p>You must read and fully understand Snips voice system before starting this</p>
<p>Please read the documentation of snips to understand it <a href="https://console.snips.ai/login">https://console.snips.ai/login</a></p>
</div>
<div class="admonition info">
<p class="admonition-title">How to set it up?</p>
<p>This demo is already installed on your QTrobot. you can find it under ~/robot/code/qt_app</p>
<p><em>Snips already installed on QTRP (head)</em></p>
<ul>
<li>how to run the snip servers:
<div class="codehilite"><pre><span></span><code><span class="err">sudo systemctl start snips-asr.service snips-audio-server.service  snips-dialogue.service snips-hotword.service snips-nlu.service</span>
</code></pre></div></li>
<li>
<p>how to run qt_voice_app demo
<div class="codehilite"><pre><span></span><code><span class="err">rosrun qt_voice_app qt_voice_app.py</span>
</code></pre></div></p>
</li>
<li>
<p>Voice commands</p>
</li>
<li>Hey QT, show me your happy emotion</li>
<li>
<p>Hey QT, play happy gesture</p>
</li>
<li>
<p>Snips configuration file
<div class="codehilite"><pre><span></span><code><span class="err">/etc/snips.toml</span>
</code></pre></div></p>
</li>
</ul>
</div>
<h2 id="code">Code</h2>
<details class="abstract"><summary>Check code here</summary><div class="codehilite"><pre><span></span><code>        <span class="c1">#!/usr/bin/env python2</span>
    <span class="c1"># -*- coding: utf-8 -*-</span>

    <span class="kn">import</span> <span class="nn">time</span>
    <span class="kn">import</span> <span class="nn">threading</span>
    <span class="kn">from</span> <span class="nn">hermes_python.hermes</span> <span class="kn">import</span> <span class="n">Hermes</span>

    <span class="kn">import</span> <span class="nn">rospy</span>
    <span class="kn">from</span> <span class="nn">std_msgs.msg</span> <span class="kn">import</span> <span class="n">String</span>
    <span class="kn">from</span> <span class="nn">qt_gesture_controller.srv</span> <span class="kn">import</span> <span class="o">*</span>
    <span class="kn">from</span> <span class="nn">qt_motors_controller.srv</span> <span class="kn">import</span> <span class="o">*</span>
    <span class="kn">from</span> <span class="nn">qt_robot_interface.srv</span> <span class="kn">import</span> <span class="o">*</span>


    <span class="n">talkText</span> <span class="o">=</span> <span class="n">rospy</span><span class="o">.</span><span class="n">ServiceProxy</span><span class="p">(</span><span class="s1">&#39;/qt_robot/behavior/talkText&#39;</span><span class="p">,</span> <span class="n">behavior_talk_text</span><span class="p">)</span>
    <span class="n">gesturePlay</span> <span class="o">=</span> <span class="n">rospy</span><span class="o">.</span><span class="n">ServiceProxy</span><span class="p">(</span><span class="s1">&#39;/qt_robot/gesture/play&#39;</span><span class="p">,</span> <span class="n">gesture_play</span><span class="p">)</span>
    <span class="n">gestureList</span> <span class="o">=</span> <span class="n">rospy</span><span class="o">.</span><span class="n">ServiceProxy</span><span class="p">(</span><span class="s1">&#39;/qt_robot/gesture/list&#39;</span><span class="p">,</span> <span class="n">gesture_list</span><span class="p">)</span>
    <span class="n">gestureSave</span> <span class="o">=</span> <span class="n">rospy</span><span class="o">.</span><span class="n">ServiceProxy</span><span class="p">(</span><span class="s1">&#39;/qt_robot/gesture/save&#39;</span><span class="p">,</span> <span class="n">gesture_save</span><span class="p">)</span>
    <span class="n">setControlMode</span> <span class="o">=</span> <span class="n">rospy</span><span class="o">.</span><span class="n">ServiceProxy</span><span class="p">(</span><span class="s1">&#39;/qt_robot/motors/setControlMode&#39;</span><span class="p">,</span> <span class="n">set_control_mode</span><span class="p">)</span>
    <span class="n">gestureRecord</span> <span class="o">=</span> <span class="n">rospy</span><span class="o">.</span><span class="n">ServiceProxy</span><span class="p">(</span><span class="s1">&#39;/qt_robot/gesture/record&#39;</span><span class="p">,</span> <span class="n">gesture_record</span><span class="p">)</span>

    <span class="c1">#gesturePlay_pub = rospy.Publisher(&#39;/qt_robot/gesture/play&#39;, String, queue_size=10)</span>
    <span class="n">emotionShow_pub</span> <span class="o">=</span> <span class="n">rospy</span><span class="o">.</span><span class="n">Publisher</span><span class="p">(</span><span class="s1">&#39;/qt_robot/emotion/show&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">,</span> <span class="n">queue_size</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>

    <span class="n">action_thread</span> <span class="o">=</span> <span class="kc">None</span>


    <span class="k">def</span> <span class="nf">emotion_intent_callback</span><span class="p">(</span><span class="n">hermes</span><span class="p">,</span> <span class="n">intent_message</span><span class="p">):</span>
        <span class="c1">#hermes.publish_end_session(intent_message.session_id, None)</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;in emotion_intent_callback&quot;</span><span class="p">)</span>    
        <span class="k">if</span> <span class="n">intent_message</span><span class="o">.</span><span class="n">slots</span><span class="p">:</span>
            <span class="k">for</span> <span class="p">(</span><span class="n">slot_value</span><span class="p">,</span> <span class="n">slot</span><span class="p">)</span> <span class="ow">in</span> <span class="n">intent_message</span><span class="o">.</span><span class="n">slots</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="k">if</span> <span class="n">slot_value</span> <span class="o">==</span> <span class="s2">&quot;ShowEmotion&quot;</span><span class="p">:</span>
                    <span class="n">emotion_name</span> <span class="o">=</span> <span class="n">slot</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">slot_value</span><span class="o">.</span><span class="n">value</span><span class="o">.</span><span class="n">value</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Showing emotion </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="s2">&quot;QT/</span><span class="si">%s</span><span class="s2">&quot;</span><span class="o">%</span><span class="n">emotion_name</span><span class="p">)</span>
                        <span class="n">talkText</span><span class="p">(</span><span class="s2">&quot;This is my </span><span class="si">%s</span><span class="s2"> face.&quot;</span> <span class="o">%</span> <span class="n">emotion_name</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s2">&quot;_&quot;</span><span class="p">,</span> <span class="s2">&quot; &quot;</span><span class="p">))</span>
                        <span class="n">emotionShow_pub</span><span class="o">.</span><span class="n">publish</span><span class="p">(</span><span class="s2">&quot;QT/</span><span class="si">%s</span><span class="s2">&quot;</span><span class="o">%</span><span class="n">emotion_name</span><span class="p">)</span>
                    <span class="k">except</span> <span class="n">rospy</span><span class="o">.</span><span class="n">ServiceException</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
                        <span class="nb">print</span> <span class="s2">&quot;Service call failed: </span><span class="si">%s</span><span class="s2">.&quot;</span> <span class="o">%</span> <span class="n">e</span>
                        <span class="n">talkText</span><span class="p">(</span><span class="s2">&quot;I have a problem for showing emotion </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">emotion_name</span><span class="p">)</span>                                    
        <span class="k">else</span><span class="p">:</span>
            <span class="n">talkText</span><span class="p">(</span><span class="s2">&quot;I did not understand which emotion.&quot;</span><span class="p">)</span>
            <span class="n">emotionShow_pub</span><span class="o">.</span><span class="n">publish</span><span class="p">(</span><span class="s2">&quot;QT/confused&quot;</span><span class="p">)</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;done!&quot;</span><span class="p">)</span>


    <span class="k">def</span> <span class="nf">play_intent_callback</span><span class="p">(</span><span class="n">hermes</span><span class="p">,</span> <span class="n">intent_message</span><span class="p">):</span>
        <span class="c1">#hermes.publish_end_session(intent_message.session_id, None)</span>
        <span class="c1"># action code goes here...</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;in play_intent_callback&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">intent_message</span><span class="o">.</span><span class="n">slots</span><span class="p">:</span>
            <span class="k">for</span> <span class="p">(</span><span class="n">slot_value</span><span class="p">,</span> <span class="n">slot</span><span class="p">)</span> <span class="ow">in</span> <span class="n">intent_message</span><span class="o">.</span><span class="n">slots</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="k">if</span> <span class="n">slot_value</span> <span class="o">==</span> <span class="s2">&quot;GastureName&quot;</span><span class="p">:</span>
                    <span class="n">gesture_name</span> <span class="o">=</span> <span class="n">slot</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">slot_value</span><span class="o">.</span><span class="n">value</span><span class="o">.</span><span class="n">value</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="n">res</span> <span class="o">=</span> <span class="n">gestureList</span><span class="p">()</span>
                        <span class="k">if</span> <span class="n">gesture_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">res</span><span class="o">.</span><span class="n">gestures</span><span class="p">:</span>
                            <span class="n">talkText</span><span class="p">(</span><span class="s2">&quot;I do not know this gesture. But you can always record your gesture. Just ask me, hey Q.T. . record, new gesture!&quot;</span><span class="p">)</span>
                        <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">gesture_name</span> <span class="o">!=</span> <span class="s2">&quot;my&quot;</span><span class="p">:</span>
                    <span class="n">gesture_name</span> <span class="o">=</span> <span class="s2">&quot;QT/&quot;</span> <span class="o">+</span> <span class="n">gesture_name</span>
                            <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;playing gesture &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span> <span class="o">%</span> <span class="n">gesture_name</span><span class="p">)</span>
                            <span class="n">res</span> <span class="o">=</span> <span class="n">gesturePlay</span><span class="p">(</span><span class="n">gesture_name</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">)</span>
                    <span class="k">except</span> <span class="n">rospy</span><span class="o">.</span><span class="n">ServiceException</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
                        <span class="nb">print</span> <span class="s2">&quot;Service call failed: </span><span class="si">%s</span><span class="s2">.&quot;</span> <span class="o">%</span> <span class="n">e</span>
                        <span class="n">talkText</span><span class="p">(</span><span class="s2">&quot;I have a problem for playing gesture </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">gesture_name</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">talkText</span><span class="p">(</span><span class="s2">&quot;I did not understand which gesture.&quot;</span><span class="p">)</span>
            <span class="n">emotionShow_pub</span><span class="o">.</span><span class="n">publish</span><span class="p">(</span><span class="s2">&quot;QT/confused&quot;</span><span class="p">)</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;done!&quot;</span><span class="p">)</span>


    <span class="k">def</span> <span class="nf">record_intent_callback</span><span class="p">(</span><span class="n">hermes</span><span class="p">,</span> <span class="n">intent_message</span><span class="p">):</span>
        <span class="c1">#hermes.publish_end_session(intent_message.session_id, None)</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;in record_intent_callback&quot;</span><span class="p">)</span>
        <span class="n">talkText</span><span class="p">(</span><span class="s2">&quot;You can start recording your gesture.&quot;</span><span class="p">)</span>
        <span class="n">res</span> <span class="o">=</span> <span class="n">gestureRecord</span><span class="p">([</span><span class="s2">&quot;right_arm&quot;</span><span class="p">,</span> <span class="s2">&quot;left_arm&quot;</span><span class="p">],</span> <span class="kc">True</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;done!&quot;</span><span class="p">)</span>


    <span class="k">def</span> <span class="nf">stop_intent_callback</span><span class="p">(</span><span class="n">hermes</span><span class="p">,</span> <span class="n">intent_message</span><span class="p">):</span>
        <span class="c1">#hermes.publish_end_session(intent_message.session_id, None)</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;in stop_intent_callback&quot;</span><span class="p">)</span>    
        <span class="n">res</span> <span class="o">=</span> <span class="n">gestureSave</span><span class="p">(</span><span class="s2">&quot;my&quot;</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">)</span>    
        <span class="k">if</span> <span class="ow">not</span> <span class="n">res</span><span class="o">.</span><span class="n">status</span><span class="p">:</span>
            <span class="n">talkText</span><span class="p">(</span><span class="s2">&quot;I have a problem with recording the gesture&quot;</span><span class="p">)</span>            
        <span class="n">setControlMode</span><span class="p">([</span><span class="s2">&quot;right_arm&quot;</span><span class="p">,</span> <span class="s2">&quot;left_arm&quot;</span><span class="p">],</span> <span class="mi">1</span><span class="p">)</span>
        <span class="n">talkText</span><span class="p">(</span><span class="s2">&quot;Gesture recorded!&quot;</span><span class="p">)</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;done!&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">unknown_intent_callback</span><span class="p">(</span><span class="n">hermes</span><span class="p">,</span> <span class="n">intent_message</span><span class="p">):</span>
        <span class="n">talkText</span><span class="p">(</span><span class="s2">&quot;I do not know this. But you can ask me to play gesture or show emotions.&quot;</span><span class="p">)</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;done&quot;</span><span class="p">)</span>


    <span class="k">def</span> <span class="nf">intent_received</span><span class="p">(</span><span class="n">hermes</span><span class="p">,</span> <span class="n">intent_message</span><span class="p">):</span>
            <span class="n">hermes</span><span class="o">.</span><span class="n">publish_end_session</span><span class="p">(</span><span class="n">intent_message</span><span class="o">.</span><span class="n">session_id</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
            <span class="n">coming_intent</span> <span class="o">=</span> <span class="n">intent_message</span><span class="o">.</span><span class="n">intent</span><span class="o">.</span><span class="n">intent_name</span>
            <span class="nb">print</span><span class="p">(</span><span class="n">intent_message</span><span class="o">.</span><span class="n">intent</span><span class="o">.</span><span class="n">confidence_score</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">intent_message</span><span class="o">.</span><span class="n">intent</span><span class="o">.</span><span class="n">confidence_score</span> <span class="o">&lt;</span> <span class="mf">0.5</span><span class="p">:</span>
                <span class="n">action_thread</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">unknown_intent_callback</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">hermes</span><span class="p">,</span> <span class="n">intent_message</span><span class="p">))</span>
                <span class="n">action_thread</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
                <span class="k">return</span>

            <span class="nb">print</span>
            <span class="k">if</span> <span class="n">coming_intent</span> <span class="o">==</span> <span class="s1">&#39;apaikan:Play&#39;</span><span class="p">:</span>
                <span class="n">action_thread</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">play_intent_callback</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">hermes</span><span class="p">,</span> <span class="n">intent_message</span><span class="p">))</span>
                <span class="n">action_thread</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>            
            <span class="k">elif</span> <span class="n">coming_intent</span> <span class="o">==</span> <span class="s1">&#39;apaikan:Record&#39;</span><span class="p">:</span>
                <span class="n">action_thread</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">record_intent_callback</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">hermes</span><span class="p">,</span> <span class="n">intent_message</span><span class="p">))</span>
                <span class="n">action_thread</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>                        
            <span class="k">elif</span> <span class="n">coming_intent</span> <span class="o">==</span> <span class="s1">&#39;apaikan:Emotion&#39;</span><span class="p">:</span>
                <span class="n">action_thread</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">emotion_intent_callback</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">hermes</span><span class="p">,</span> <span class="n">intent_message</span><span class="p">))</span>
                <span class="n">action_thread</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>                                    
            <span class="k">elif</span> <span class="n">coming_intent</span> <span class="o">==</span> <span class="s1">&#39;apaikan:Stop&#39;</span><span class="p">:</span>
                <span class="n">action_thread</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">stop_intent_callback</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">hermes</span><span class="p">,</span> <span class="n">intent_message</span><span class="p">))</span>
                <span class="n">action_thread</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">action_thread</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">unknown_intent_callback</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">hermes</span><span class="p">,</span> <span class="n">intent_message</span><span class="p">))</span>
                <span class="n">action_thread</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>


    <span class="k">def</span> <span class="nf">intent_not_recognized</span><span class="p">(</span><span class="n">hermes</span><span class="p">,</span> <span class="n">intent_nr_message</span><span class="p">):</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;not recogniozed&quot;</span><span class="p">)</span>
        <span class="n">action_thread</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">unknown_intent_callback</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">hermes</span><span class="p">,</span> <span class="n">intent_nr_message</span><span class="p">))</span>
        <span class="n">action_thread</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>


    <span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&quot;__main__&quot;</span><span class="p">:</span>
        <span class="c1"># call the relevant service</span>
        <span class="n">rospy</span><span class="o">.</span><span class="n">init_node</span><span class="p">(</span><span class="s1">&#39;qt_voice_interface&#39;</span><span class="p">,</span> <span class="n">disable_signals</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="k">with</span> <span class="n">Hermes</span><span class="p">(</span><span class="s1">&#39;localhost:1883&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">h</span><span class="p">:</span>  
    <span class="c1">#        h.subscribe_intent(&quot;apaikan:Play&quot;, play_intent_callback)</span>
    <span class="c1">#        h.subscribe_intent(&quot;apaikan:Record&quot;, record_intent_callback)</span>
    <span class="c1">#        h.subscribe_intent(&quot;apaikan:Stop&quot;, stop_intent_callback)        </span>
    <span class="c1">#        h.subscribe_intent(&quot;apaikan:Emotion&quot;, emotion_intent_callback)                </span>
            <span class="n">h</span><span class="o">.</span><span class="n">subscribe_intents</span><span class="p">(</span><span class="n">intent_received</span><span class="p">)</span>
            <span class="n">h</span><span class="o">.</span><span class="n">subscribe_intent_not_recognized</span><span class="p">(</span><span class="n">intent_not_recognized</span><span class="p">)</span>        
            <span class="n">h</span><span class="o">.</span><span class="n">loop_forever</span><span class="p">()</span>
            <span class="c1"># async mode  using</span>
            <span class="c1">#h.loop_start() rospy.spin() h.loop_stop()</span>
</code></pre></div>

</details>
                
                  
                
                
                  
                  <hr>
                  <div class="md-source-date">
                    <small>
                      
                        Last update: February 5, 2020
                      
                    </small>
                  </div>
                
              
              
                


              
            </article>
          </div>
        </div>
      </main>
      
        <!--
  Copyright (c) 2016-2020 Martin Donath <martin.donath@squidfunk.com>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to
  deal in the Software without restriction, including without limitation the
  rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  sell copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  IN THE SOFTWARE.
-->



<!-- Application footer -->
<footer id="footer" class="md-footer">

  <!-- Link to previous and/or next page -->
  

  <!-- Further information -->
  <div class="md-footer-meta md-typeset">
    <div class="md-footer-meta__inner md-grid">
      <!-- Copyright and theme information -->
      <div class="md-footer-copyright">
        
          <div class="md-footer-copyright__highlight">
            &copy; Copyright &copy; 2020 LuxAI S.A.
          </div>
        
      </div>

      <!-- Social links -->
      
  <div class="md-footer-social">
    <link rel="stylesheet" href="../../assets/fonts/font-awesome.css">
    
      <a href="https://github.com/luxai-qtrobot" target="_blank" rel="noopener" title="github" class="md-footer-social__link fa fa-github"></a>
    
      <a href="https://twitter.com/qtrobot" target="_blank" rel="noopener" title="twitter" class="md-footer-social__link fa fa-twitter"></a>
    
      <a href="https://www.linkedin.com/company/luxai/" target="_blank" rel="noopener" title="linkedin" class="md-footer-social__link fa fa-linkedin"></a>
    
      <a href="https://www.facebook.com/luxAI/" target="_blank" rel="noopener" title="facebook" class="md-footer-social__link fa fa-facebook"></a>
    
      <a href="https://www.youtube.com/channel/UCAY0H0HaqQL1GE7lO_iSpbg" target="_blank" rel="noopener" title="youtube" class="md-footer-social__link fa fa-youtube"></a>
    
  </div>

    </div>
  </div>
</footer>
      
    </div>
    
      <script src="../../assets/javascripts/application.c33a9706.js"></script>
      
      <script>app.initialize({version:"1.1",url:{base:"../.."}})</script>
      
        <script src="../../javascripts/extra.js"></script>
      
    
  </body>
</html>